package com.itheima.travel.controller;

import com.itheima.travel.domain.Category;
import com.itheima.travel.domain.ResultInfo;
import com.itheima.travel.domain.User;
import com.itheima.travel.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public ResultInfo register(@RequestBody User user, String smsCode, HttpSession session) {
        String sessionSmsCode = (String) session.getAttribute("smsCode" + user.getTelephone());
        if (!sessionSmsCode.equalsIgnoreCase(smsCode)) {
            return new ResultInfo(false, "验证码错误");
        } else {
            session.removeAttribute("smsCode" + user.getTelephone());
        }
        return userService.register(user);

    }

    @RequestMapping("/sendSms")
    public ResultInfo sendCode(String telephone, HttpSession session) {
        String authCode = RandomStringUtils.randomNumeric(6);
//        String authCode = RandomStringUtils.random(6);
//        String authCode = code;

        System.out.println("发送的验证码 = " + authCode);
        ResultInfo resultInfo = userService.sendSms(telephone, authCode);
        if (resultInfo.getSuccess()) {
            session.setAttribute("smsCode" + telephone, authCode);
        }
        return resultInfo;
    }

    @RequestMapping("/findByUsername")
    public ResultInfo findByUserName(String username) {
        User dbUser = userService.findByUserName(username);
        if (dbUser == null) {
            return new ResultInfo(true, "用户名可用");
        } else {
            return new ResultInfo(false, "用户名已经被占用");
        }
    }

    //    根据用户名发送验证码
    @RequestMapping("/loginSendMessage")
    public ResultInfo loginSendMessage(String username) {
//        根据用户名查找一个用户
        User user = userService.findByUserName(username);
        if (user == null) {
            return new ResultInfo(false, "用户名不存在");
        }
        String smsCode = RandomStringUtils.randomNumeric(6);
        System.out.println("发送的验证码== " + smsCode);
        redisTemplate.opsForValue().set("smsCode" + user.getTelephone(), smsCode);
        ResultInfo resultInfo = userService.sendSms(user.getTelephone(), smsCode);
        return resultInfo;


    }

    @RequestMapping("/login")
    public ResultInfo login(@RequestBody Map<String, String> map, HttpSession session) {
        ResultInfo resultInfo = userService.login(map);
        if (resultInfo.getSuccess()) {
            session.setAttribute("user", resultInfo.getData());
        }
        return resultInfo;
    }

    @RequestMapping("/isLogged")
    public ResultInfo isLogged(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return new ResultInfo(false, "未登录");
        } else {
            return new ResultInfo(true, user);
        }
    }

    @RequestMapping("/logout")
    public void logout(HttpSession session) {
        session.invalidate();
    }

}
